<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>ws client</title>
</head>
<body>
<p style="border:1px solid black;width: 600px;height: 500px" id="talkMsg"></p>
<input id="message" /><button id="sendBtn" onclick="sendMsg()">发送</button>
</body>
<script>
    // 用户登录后获取的值
    let username = "wj"; // 这个值应该是从登录流程中获取的

    // encodeURIComponent确保使用了正确的编码格式
    let wsUrl = `ws://localhost:8080/myWs/${encodeURIComponent(username)}`;

    // 创建 WebSocket 连接
    let ws = new WebSocket(wsUrl);
    // let ws = new WebSocket("ws://localhost:8080/myWs")
    // ws.onopen=function () {
    //     //发送请求获取数据库的信息，传递给后端
    //     ws.send(JSON.stringify({ action: 'connect', userId: 32 }));
    //     // alert("成功登录"+event.AT_TARGET);
    // }

    ws.onmessage=function (message) {
        var talkMsgElement = document.getElementById('talkMsg');
        // var parse = JSON.parse(message.data);
        // talkMsgElement.innerHTML +=  parse.name;
        talkMsgElement.innerHTML +=  message.data;
    }
    // wx.onclose=function () {
    //     alert("连接关闭")
    // }
    function sendMsg() {
        ws.send(JSON.stringify({userName:"wj",userTo:"-1", text:document.getElementById("message").value}));
        document.getElementById("message").value=""
    }
</script>
</html>